package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.NewActVH;
import com.centaline.androidsalesblog.model.businissobject.NewPropImgBo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstActDo;
import com.centaline.androidsalesblog.utils.DateUtil;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 我的活动
 * <p/>
 * Created by fusl1 on 2015/12/23.
 * Modify by Lihq on 2016/01/21
 */
public class NewEstActAdapter extends BaseAdapter1<NewActVH> {

    private final DrawableRequestBuilder<String> requestBuilder;
    private final ArrayList<NewEstActDo> list;
    private String newPropImg;

    public NewEstActAdapter(Context context, DrawableRequestBuilder<String> requestBuilder, ArrayList<NewEstActDo> list) {
        super(context);
        this.requestBuilder = requestBuilder;
        this.list = list;
    }

    public void setNewPropImg(String newPropImg) {
        this.newPropImg = newPropImg;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_new_est_act;
    }

    @Override
    protected NewActVH viewHolder(View view) {
        return new NewActVH(view);
    }

    @Override
    public void onBindViewHolder(NewActVH holder, int position) {
        final NewEstActDo newEstActDo = list.get(position);
        final ArrayList<NewPropImgBo> newPropImgBos = newEstActDo.getNewPropImgBos();
        String imgAddr = newPropImgBos == null || newPropImgBos.size() == 0 ? "" : newPropImgBos.get(0).getFileUrl();
        if (TextUtils.isEmpty(newPropImg) ||
                TextUtils.isEmpty(imgAddr) ||
                !imgAddr.contains(".")) {
            holder.img_logo.setVisibility(View.INVISIBLE);
        } else {
            imgAddr = imgAddr.replace(".", "_213x138_f.");
            GlideLoad.load(new GlideLoad.Builder(requestBuilder,
                    String.format(Locale.CHINA, "%s%s", newPropImg, imgAddr))
                    .placeHolder(R.drawable.ic_centa_square_logo)
                    .error(R.drawable.ic_centa_square_logo)
                    .openWifiSwitch()
                    .into(holder.img_logo));
        }
        holder.atv_title.setText(newEstActDo.getActTitle());
        holder.atv_content.setText(newEstActDo.getDescription());
        holder.atv_date.setText(Html.fromHtml(DateUtil.getRemainTime(newEstActDo.getEndDate())));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
